package com.atguigu.eduservice.controller.front;


import com.aiguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.dto.chapter.ChapterVo;
import com.atguigu.eduservice.entity.frontvo.CourseQueryVo;
import com.atguigu.eduservice.entity.frontvo.CourseWebVo;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.servicebase.entity.vo.CourseOrderVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Y丶HS练习谷粒商城
 * @since 2022-09-24
 */
@Api(description = "课程前台展示控制")
@RestController
@RequestMapping("/eduservice/admin/course")
public class AdminEduCourseController {

    @Resource
    private EduCourseService eduCourseService;

    @Resource
    private EduChapterService eduChapterService;

    /**
     * 1 查询讲师表的所有数据
     *
     * @return
     */
    @ApiOperation("查询课程的热点数据 8条数据")
    @GetMapping("/listEntity")
    public R listEntity() {
        List<EduCourse> list = eduCourseService.listByLimit4();
        return R.ok().data("info", list);
    }

    @PostMapping("/getCourseFrontList/{page}/{limit}")
    public R getCourseFrontList(@ApiParam(name = "page", value = "当前页码", required = true)
                                @PathVariable
                                        Long page,
                                @ApiParam(name = "limit", value = "每页记录数", required = true)
                                @PathVariable Long limit,
                                @ApiParam(name = "courseQuery", value = "查询对象", required = false)
                                @RequestBody(required = false) CourseQueryVo courseQuery
    ) {
        // 创建分页对象
        Page<EduCourse> coursePage = new Page<>(page, limit);
        // 调用方法进行分页，分页数据封装到teacherPage对象里面
        Map<String, Object> map = eduCourseService.getCourseFrontList(coursePage, courseQuery);
        return R.ok().data(map);
    }

    /**
     * 课程详情
     */
    @GetMapping("/getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(
            @PathVariable("courseId") String courseId
    ){
        // 获取课程信息
        CourseWebVo courseWebVo = eduCourseService.getBaseCourseById(courseId);

        // 获取小节等信息
        List<ChapterVo> chapterVos = eduChapterService.getCharterVideoById(courseId);

        return R.ok().data("course",courseWebVo).data("chapterList",chapterVos);
    }

    @ApiOperation("订单：课程信息")
    @PostMapping("/getCourseInfoOrder/{id}")
    public CourseOrderVo getCourseInfoOrder(@PathVariable String id){
        CourseWebVo courseWebVo = eduCourseService.getBaseCourseById(id);
        CourseOrderVo courseOrderVo = new CourseOrderVo();
        BeanUtils.copyProperties(courseWebVo,courseOrderVo);
        return courseOrderVo;
    }
}

